<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>应急信息发布项目维护修改</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/iconfont/iconfont.css" rel="stylesheet" type="text/css">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script>
        layui.use(['table', 'form', 'jquery', 'element','layer','laydate'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.$;
            var layer=layui.layer;
            var laydate = layui.laydate;
            var user;
            var mgrId = getUrlParam("mgrId");
            var mgrState = getUrlParam("mgrState");
            var checkState = getUrlParam("checkState");
            var reset = 0;
            var mapManage = "/mapManage.html";
            var smsRealtimeControl;
	        laydate.render({
	             elem: '#startTime'
	             , type: 'datetime'
	             ,min:minDate()
	        });
	        laydate.render({
	             elem: '#endTime'
	             , type: 'datetime'
	             ,min:minDate()
	        });
	        /* form.on('radio(localDate1)', function(data){
                if(data.value==0||data.value==2){
                    console.info(data.value);
                    $("#local").show();


                }else {
                    console.info(data.value);
                    $("#local").hide();
                }
            }); */
	        
	        function minDate(){
			    var now = new Date();
			    return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
			}
	        
            //跳转地图
            $('#ditubtn').on('click', function () {
           	 	//var xiangmuid=$("#xiangmuid").val();
           	 	var startTime=$("#startTime").val();
                var endTime=$("#endTime").val();
                var countyId = $("#countyIdValue").val();
                var regionName = $("#countyId").val();
	            var isChooseMap = $("input[name='isChooseMap']:checked").val();
	            var cityId=$("#cityId").val();
	            var regionTypeValue = $("#quyudetail").val();
	            var quyutype=$("#quyutype").val();
	            if(isNull(cityId)){
                    cityId="25";
                }
                if(isNull(mgrId)){
                    layer.msg("未获取到项目编号，请重新加载");
                    return false;
                }
		        if(isChooseMap==1){
		            var url=mapManage+"?cityId="+cityId+"&mgrId="+mgrId+"&sendStart="+startTime+"&sendEnd="+endTime
		            		+"&scopeType="+"1"+"&countyId="+countyId+"&mgrState="+mgrState+"&checkState="+checkState
		            		+"&regionName="+encodeURI(regionName)+"&regionTypeValue="+regionTypeValue+"&flag="+"modify"+"&resultRegionType="+quyutype;
	                url=encodeURI(url); 
		            layer.open({
	                    title: '选取发送区域',
	                    type: 2,
	                    content: url,
	                    //area: ['1200px', '650px'],
	                    area: ['90%','90%'],
	                    scrollbar: true
	                });
		        }else if(isChooseMap==2){
		            var url="/countyManage.html?cityId="+cityId+"&mgrId="+mgrId+"&sendStart="+startTime+"&sendEnd="+endTime
		            		+"&scopeType="+"0"+"&countyId="+countyId+"&mgrState="+mgrState+"&checkState="+checkState
		            		+"&regionTypeValue="+regionTypeValue+"&flag="+"modify";
		            layer.open({
	                    title: '选取发送区域',
	                    type: 2,
	                    content: url,
	                    //area: ['1200px', '650px'],
	                    area: ['90%','90%'],
	                    scrollbar: true
	                });
		        }
            });
            //插入提交
            $('#submit').on('click', function () {
                var cityId=$("#cityId").val();
                if (isNull(cityId)) {
					cityId="25";
                }
                var countyId = $("#countyId").val();
                var name = $("#name").val();
                var extId=$("#extId").val();
                var startTime=$("#startTime").val();
                var endTime=$("#endTime").val();
                var content=$("#content").val();
                /* var cycle = $("input[name='cycle']:checked").val();     //发送周期
                var cycleThreshold=$("#cycleThreshold").val(); */
                var localDate=$("input[name='localDate']:checked").val();
                var isThreshold = 0;
                $("input:checkbox[name='isThreshold']:checked").each(function () {
                    isThreshold += parseInt($(this).val());
                });
                var cycleThreshold=0;
                var cycle;
                if(isThreshold==2||isThreshold==3){
                	//var cycle = $("input[name='cycle']:checked").val();     //发送周期
                	cycle = $("#cycle").val();
                	cycleThreshold=$("#cycleThreshold").val();
                }
                var isChooseMap = $("input[name='isChooseMap']:checked").val();
		        var xiangmuid=$("#xiangmuid").val();
                var quyuid=$("#quyuid").val();
                var quyutype=$("#quyutype").val();
                var quyudetail=$("#quyudetail").val();
                var param = {
                    cityId: cityId,
                    countyId: countyId,
                    name: name,
                    extId: extId,
                    startTime: startTime,   //项目开始时间
                    endTime: endTime,//结束时间
                    isLocal: $("input[name='isLocal']:checked").val(),//是否本地发送
                    threshold: $("#threshold").val(),  //全部阀值
                    isThreshold: isThreshold,
                    content:content,
                    mgrId:xiangmuid,
                    regionId:quyuid,
                    regionType:quyutype,
                    regionTypeValue:quyudetail,
                    reset:reset,
                    cycle:cycle,
                    cycleThreshold: cycleThreshold,//周期阀值
                    localDate:localDate,
                    flag: "modify"
                };
                var jsonParams=JSON.stringify(param);
		        //alert("jsonParams:"+jsonParams);
                if (isNull(name)) {
                    layer.msg("项目名称不能为空");
                    return false;
                }
                if (startTime == null || startTime== "") {
                    layer.msg("发送开始时间不能为空");
                    return false;
                }
                if (endTime == null || endTime == "") {
                    layer.msg("发送结束时间不能为空");
                    return false;
                }
                if (startTime>=endTime) {
                    layer.msg("发送结束时间不能小于等于开始时间");
                    return false;
                }
                if (!checkRate($("#threshold").val())) {
                    layer.msg("阀值必须为数字,若无阀值填0");
                    return false;
                }
                if($("#threshold").val()>0&&isThreshold==0){
                    layer.msg("若设置阈值请勾选“短信发送总量”，若不设置阈值请填0");
                    return false;
                }
                if (isNull(content)) {
                    layer.msg("短信内容不能为空");
                    return false;
                }
                if (isNull(xiangmuid)||isNull(quyuid)||isNull(quyutype)||isNull(quyudetail)) {
                    layer.msg("区域信息有误，请重新选择区域");
                    return false;
                }
                if (isThreshold < 2 && $("#cycleThreshold").val() > 0) {
                    layer.confirm('短信周期发送量大于0，但并未勾选\'周期短信发送量\',按周期设置阀值不会生效,您是否要继续？', {
                        btn: ['继续', '取消'] //按钮
                    }, function () {
                        jugdge(param);
                    });
                    return false;
                } else if (isThreshold != 1 && isThreshold != 3 && $("#threshold").val() > 0) {
                    layer.confirm('短信发送总条数大于0，但并未勾选\'短信发送总量\',总量阀值设置不会生效,您是否要继续？', {
                        btn: ['继续', '取消']//按钮
                    }, function () {
                        jugdge(param);
                    });
                    return false;
                } else {
                    jugdge(param);
                }
               
               
            });
            
            function jugdge(parm) {
                console.info(parm);
                //判断总阀值是否超出
                if (parm.isThreshold == 1) {
                    console.info("总值判断");
                    if (smsRealtimeControl.totalCount-parm.threshold >=0 && smsRealtimeControl.totalCount != 0) {
                        console.info("总值不满足");
                        layer.confirm("目前发送的数量已经超出了阀值的数量，是否需要清零？", {
                            btn: ['是', '否，重新设置阀值'] //按钮
                        }, function () {
                            reset = 1;
                            submitParm(parm);
                        });
                    } else {
                         submit(parm);
                    }
                }else {
                    submit(parm);
                }
            }
            function submit(parm) {
                layer.confirm('是否需要重置以前的发送量,使发送量从零开始计算吗？', {
                    btn: ['需要', '不需要'] //按钮
                }, function () {
                    reset = 1;
                    submitParm(parm, reset);
                }, function () {
                    submitParm(parm, reset);
                });
            }
            function submitParm(parm) {
                layer.confirm('您确认输入的信息无误吗？', {
                    btn: ['确认提交', '再看看']
                }, function () {
                    parm.reset = reset;
                    console.info(parm);    				
                    var url='/mgrTemplate/configureMgrTemplate';
                    $.ajax({
                        type: 'post',
                        data: parm,
                        url: url,
                        beforeSend: function () {
                            loading = layer.load(0, {shade: false});
                        },
                        success: function (json) {
                            layer.close(loading);
                            if (json.code == 0) {
                                layui.use('layer', function () {
    								layer.msg('配置成功,点击确认返回短信项目维护界面', {    		
                                        time: 0 //不自动关闭
                                        , btn: ['确定']
                                        , yes: function (index) {
                                            layer.close(index);
                                            location.href = "smsMgrTemplateManage.html";                                           
    									}                                      
                                    });
                                });
                            } else {
                                layer.msg(json.msg);
    						}
    						//变更同步
    						var turl = "/SmsChange/findChangeByMgrId";
    						var tparam = {
    							mgrId : mgrId
    						};
    						$.ajax({
    							type : 'post',
    							data : tparam,
    							url : turl,
    							success : function(json) {
    								console.info(json);
    							}
    						})
    
    					},
                        complete: function () {
                        },
                        error: function (data) {
                        	layer.close(loading);
                            layer.msg('系统异常，请联系管理员处理!');
    					}
    				});   
    			}, function() {
    				reset = 0;
                })
            }
            function getUserFlag(){
             $.ajax({
                    type: 'post',
                    async: false,
                    url: '/util/findUser',
                    success: function (json) {
                        user=json;
                        console.info(json);
                        if(json.specialFlag == 1){
                        	mapManage = "../areaManage/xuYiMapManage.html";
                        }
                        }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            }
            
            //页面数据初始化
            $(document).ready(function () {
            getUserFlag();
                var url = '/mgrTemplate/findMgrTemplateByMgrId';
                var param = {
                    mgrId: mgrId
                };
                $.ajax({
                    type: 'post',
                    async: false,
                    url: url,
                    data: param,
                    beforeSend: function () {
                        loading = layer.load(0, {shade: false});
                    },
                    success: function (json) {
                        layer.close(loading);
                        var data = json.data;
                        smsRealtimeControl = data.smsRealtimeControl;
                        $("#cityId").val(data.cityId);
                        $("#countyId").val(data.countyName);
                        $("#countyIdValue").val(data.countyId);
                        if(data.cityExtId.length==5){
                            $("#extId").val(data.cityExtId.substring(data.cityExtId.length-3));
                        }
                     	$("#name").val(data.name);
                     	$("#startTime").val(data.startDate);
                     	$("#endTime").val(data.endDate);
                        $("#content").val(data.content);
                        $("#xiangmuid").val(mgrId);
                        $("#quyuid").val(data.regionId);
                        $("#quyutype").val(data.regionType);
                        $("#quyudetail").val(data.regionTypeValue);
                        var regionTypeFlag = data.regionType;
                        if(regionTypeFlag==2||regionTypeFlag==3||regionTypeFlag==4){
                        	$("input[name='isChooseMap'][value=" + 1 + "]").attr("checked", true);
                        }else{
                        	$("input[name='isChooseMap'][value=" + 2 + "]").attr("checked", true);
                        }
                     	$("input[name='isLocal'][value=" + data.isLocal + "]").attr("checked", true);
                     	$("input[name='localDate'][value=" + data.localDate + "]").attr("checked", true);
                     	//$("input[name='cycle'][value=" + data.cycle + "]").attr("checked", true);
                     	$("#cycle").val(data.cycle);
                     	$("#cycleThreshold").val(data.cycleThreshold);
                     	$("#threshold").val(data.threshold);
                     	var isThreshold = document.getElementsByName("isThreshold");
                     	/* if (data.isThreshold == 1) {
                     		$("input[name='isThreshold']").attr("checked", true);
                     		$("#threshold").val(data.threshold);
                        } */
                        if(data.isLocal!=1){
                            $("#local").show();
                        }
                        if (data.isThreshold == 1) {
                            isThreshold[0].checked = true;
                        } else if (data.isThreshold == 2) {
                            isThreshold[1].checked = true;
                        } else if (data.isThreshold == 3) {
                            isThreshold[0].checked = true;
                            isThreshold[1].checked = true;
                        }
                     	
                        form.render();                     
                    },
                    error: function (data) {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                });
            });
            //校验字符串
		    function checkStr(str,defaultVal){
			if (typeof(str) == "undefined" || str == null || str == "null") {
		        if (defaultVal == undefined) {
		            str = "";
		        } else {
		            str = defaultVal;
		        }
		    }
			return str;
		    }
        });

    </script>
</head>
<body>
<div class="layui-html-limit">
    <!--container-->
    <div class="layui-content-body">
        <div class="scroll-box">
            <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
                  <a href="javascript:returnIndex2()">首页&nbsp;</a>
                  <a href="#"> &nbsp;项目维护管理&nbsp; </a>
                  <a href="#"> &nbsp;短信项目维护&nbsp;</a>
                  <a href="#"> &nbsp;短信项目维护修改&nbsp; </a>
              </span>
            </div>
            <div class="content-box">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width:120px">省份</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
                                <input type="text" name="province" lay-verify="required" value="江苏"
                                       autocomplete="off" class="layui-input" disabled >
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width:120px">地市</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
                                <select name="cityId" lay-filter="cityId" id="cityId" disabled>
                                    <option value="">请选择</option>
                                    <option value="11">苏州</option>
                                    <option value="12">淮安</option>
                                    <option value="13">宿迁</option>
                                    <option value="14">南京</option>
                                    <option value="15">连云港</option>
                                    <option value="16">徐州</option>
                                    <option value="17">常州</option>
                                    <option value="18">镇江</option>
                                    <option value="19">无锡</option>
                                    <option value="20">南通</option>
                                    <option value="21">泰州</option>
                                    <option value="22">盐城</option>
                                    <option value="23">扬州</option>
                                    <option value="25">省级</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width:120px">区县</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
                                <input type="text" class="layui-input" name="mgrcountyId" lay-filter="countyId" id="countyId" disabled />
                                <input type="hidden" id="countyIdValue" disabled/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" style="width:120px">项目名称</label>
                            <div class="layui-input-block" style="width:180px;margin-left:120px;">
                                 <input type="text" name="name" id="name" required lay-verify="required"
			                           placeholder="请输入项目名称"
			                           autocomplete="off" class="layui-input" disabled>
                            </div>
                        </div>
                    </div>
                    
                    
                    <!--
			            <div class="layui-inline layui-inline-center">
			                <label class="layui-form-label">项目名称</label>
			                <div class="layui-input-block" style="width:150px">
			                    <input type="text" name="name" id="name" required lay-verify="required"
			                           placeholder="请输入项目名称"
			                           autocomplete="off" class="layui-input" disabled>
			                </div>
			            </div>
       
                         <div class="layui-inline ">
				            <label class="layui-form-label">短信扩展码</label>
				            <div class="layui-input-block" style="width:150px">
				                <input type="text" class="layui-input"  name="extId" lay-filter="extId" id="extId" disabled />
				            </div>
				        </div> -->
				   <div class="layui-form-item">
                        <div class="layui-block">
                            <label class="layui-form-label" style="width:120px">发送起止时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="date" id="startTime" autocomplete="off" class="layui-input"
                                       placeholder="请选择" lay-verify="required" style="width:180px;">
                            </div>
                            <div class="layui-form-mid">-</div>
                            <div class="layui-input-inline">
                                <input type="text" name="date" id="endTime" autocomplete="off" class="layui-input"
                                       placeholder="请选择" lay-verify="required" style="width:180px;">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label" style="width:120px">发送区域设置</label>
                        <div class="layui-input-block">
                        	<input class="radioItem" lay-filter="gis" type="radio" name="isChooseMap" title="行政区域选择" onclick="ChooseRadio(this)" value="2" checked="checked">
                            <input class="radioItem" lay-filter="gis" type="radio" name="isChooseMap" title="圈画发送范围" onclick="ChooseRadio(this)" value="1">
                        </div>
                       <!--  <div class="layui-form-item" id="quxian" style="display:none;" >
                            <div class="layui-inline">
                                <div class="layui-input-block" id="quxiancheckbox"></div>
                            </div>
                        </div> -->
                        <div class="layui-form-item" id="ditu" >
                            <div class="layui-inline">
                                <button class="layui-btn layui-btn-normal" style="margin-left: 120px;" id="ditubtn" >
                                    <i class="layui-icon">&#xe608;</i> 选择发送区域
                                </button>
                                <input type="hidden" class="layui-myinput" style="width:100px" id="xiangmuid" value="项目编码" disabled />
                                <input type="hidden" class="layui-myinput" style="width:100px" id="quyuid" value="区域编码" disabled/>
                                <input type="hidden" id="quyutype" disabled/>
                                <input type="hidden" id="quyudetail" disabled/>
                                <div id="xiangmuvalue" style="float: right;text-align: center;padding-left: 15px;margin-top: 8px;"></div>
                                
                            </div>
                             
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" style="width:120px">号码发送范围</label>
                            <div class="layui-input-block">
                                <input type="radio" name="isLocal" value="0" title="本地" lay-filter="localDate1">
                                <input type="radio" name="isLocal" value="1" title="外地" lay-filter="localDate1" checked>
                                <input type="radio" name="isLocal" value="2" title="本地+外地" lay-filter="localDate1">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item" id="local">
                        <div class="layui-block" pane="">
                            <label class="layui-form-label" style="width:120px">号码保存时间</label>
                            <div class="layui-input-block">
                                <input type="radio" name="localDate" value="1" title="1天" checked="">
                                <input type="radio" name="localDate" value="3" title="3天">
                                <input type="radio" name="localDate" value="7" title="7天">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width:120px">短信阀值设置</label>
                        <div class="layui-inline" style="height:40px;">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <input type="checkbox" name="isThreshold" title="短信发送总量" value="1">
                                </div>
                                <div class="layui-inline" pane="">
                                    <label class="layui-form-label" style="width: 150px">短信发送总条数:</label>
                                    <div class="layui-input-inline" style="width: 100px">
                                        <input type="text" id="threshold" placeholder="请输入"
                                               autocomplete="off" class="layui-input" value="0">
                                    </div>
                                </div>
                                <div class="layui-inline">条</div>
                            </div>
                        </div>
                    </div>
                    
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: auto;opacity: 0;">短信阀值设置</label>
                        <div class="layui-inline">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <input type="checkbox" name="isThreshold" title="周期发送数量" value="2">
                                </div>
                                <!-- 
                                <div class="layui-inline" pane="">
                                    <input type="radio" name="cycle" value="4" title="按日">
                             	</div>
                             	 -->
                             	 <input type="hidden" name="cycle" id="cycle" value="4" title="按日">
                                        <div class="layui-inline" pane="">
                                            <label class="layui-form-label" style="width: 150px;">日短信发送条数:</label>
                                            <div class="layui-input-inline" style="width: 100px;">
                                                <input type="text"
                                                       placeholder="请输入" id="cycleThreshold"
                                                       autocomplete="off" class="layui-input" lay-verify="number"
                                                       value="0">
                                            </div>
                                        </div>
                                        <div class="layui-inline">条</div>
                            </div>
                        </div>
                    </div>
                    <!-- <div class="layui-form-item">
                                    <input type="checkbox" name="isThreshold" title="周期短信发送量" value="2">
                    </div>
                                <div class="layui-inline" pane="">
                                    <input type="radio" name="cycle" value="1" title="按年" checked>
                                    <input type="radio" name="cycle" value="2" title="按月">
                                    <input type="radio" name="cycle" value="3" title="按周">
                                    <input type="radio" name="cycle" value="4" title="按日">
                             	</div>
                             	 <div class="layui-form-item">
                                        <div class="layui-inline" pane="">
                                            <label class="layui-form-label">短信发送量:</label>
                                            <div class="layui-input-inline" style="width: 100px;">
                                                <input type="text"
                                                       placeholder="请输入" id="cycleThreshold"
                                                       autocomplete="off" class="layui-input" lay-verify="number"
                                                       value="0">
                                            </div>
                                        </div>
                                        <div class="layui-inline">条</div>
                                    </div> -->
                    
                    <div class="layui-form-item">
                        <div class="layui-block">
                            <label class="layui-form-label" style="width: auto">发送短信内容</label>
                            <div class="layui-input-block">
                            	<textarea placeholder="请输入内容" class="layui-textarea" style="width: 50%"
                                      id="content"></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <button class="layui-btn" id="submit">立即提交</button>
                    </div>
                </div>
              
            </div>
        </div>
    </div>
</div>
</body>
</html>